package service

import (
	"fmt"

	"chat.com/library"
	"chat.com/model"
	"chat.com/register"
	"chat.com/server/grpc/grpcclient"
	"chat.com/server/wsserver/connection"
	"chat.com/setting"
)

const (
	defaultAppId = 101 // 默认平台Id
)

var (
	appIds     = []int64{defaultAppId, 102, 103, 104} // 全部的平台
	serverIp   string
	serverPort string
)

func GetAppIds() []int64 {
	return appIds
}

func GetServer() (server *model.Server) {
	server = model.NewServer(serverIp, serverPort)
	return
}

func InAppIds(appId int64) (inAppId bool) {
	for _, value := range appIds {
		if value == appId {
			inAppId = true
			return
		}
	}

	return
}

func GetDefaultAppId() (appId uint32) {
	appId = defaultAppId
	return
}

// 查询某平台所有用户，appId为0即为全平台查询
func AllUsers(appId int64) (userList []int64, userCount int) {
	userList = make([]int64, 0)
	rpcServers, err := register.ServerRegisterInstance.GetAllNodes(setting.ZookeeperSetting.RpcServerCenter)
	if err != nil {
		fmt.Println("给全体用户发消息", err)
		return
	}

	for _, server := range rpcServers {
		var (
			list []int64
		)
		if library.IsLocalServer(server) {
			list = connection.GetUserList(appId)
		} else {
			list, _ = grpcclient.GetUserList(server, appId)
		}
		userList = append(userList, list...)
	}

	userCount = len(userList)
	return
}
